const fs = require('fs');
const axios = require("axios");

const checkIn = async (cookie) => {
    return axios({
        method: 'post',
        url: 'https://glados.rocks/api/user/checkin',
        data: {
            token: "glados.network"
        },
        headers: {
            cookie
        }
    })
}

const status = async (cookie) => {
    return axios({
        method: 'get',
        url: 'https://glados.rocks/api/user/status',
        headers: {
            cookie
        }
    })
}

const GLaDOSCheckIn = async (cookie) => {
    let msg = '';
    try {
        const checkInMessage = (await checkIn(cookie))?.data?.message;
        const statusData = (await status(cookie))?.data?.data
        const leftDays = parseInt(statusData?.leftDays);
        console.log(statusData, leftDays, checkInMessage);
        msg = `
        <h1>glados 自动签到</h1>
        <ul>
        <li>签到信息: ${checkInMessage}</li>
        <li>剩余天数: ${leftDays}</li>
        </ul>
        `;
        fs.writeFileSync("index.md", msg, "utf8");
    } catch (e) {
        msg = `签到失败: ${e.toString()}`;
        fs.writeFileSync("index.md", msg, "utf8");
    }
}

const COOKIES = process.env.COOKIES;
GLaDOSCheckIn(COOKIES);